#delimit;

clear;
clear matrix;

set memory 900m;
 
set more off;
cap log close;  
set dp comma, permanently;

#delimit;

global MY_OUT_PATH  "[path where your master data set is stored]"; 
global MY_TEMP_PATH "[path where your temporary files are stored]";
global MY_FIGURE_PATH "[path where your figures and corresponding data sets are stored]";


#delimit;
use "${MY_OUT_PATH}NEW_CNEF3_LSsample.dta", clear;

************************************************;
* define weights for survey;
************************************************;
#delimit;
svyset [pweight=w11102];


*********************************************;
*********************************************;
*********************************************;

* check labor force status if working==0;
* lfs is current, while earnings are previous year - Change lfs to previous year;
#delimit;
preserve;
keep persnr year lfp;
replace year=year+1;
rename lfp lfp_new;
save "${MY_TEMP_PATH}changed_lfp", replace;
restore;

merge 1:1 persnr year using "${MY_TEMP_PATH}changed_lfp";
drop _merge;
drop lfp;

tab lfp_new if working==0;

**********************************************;
* sample selection;
*********************************************;

*keep houseeholds in the sample where at least one member is either working or unemployed (at least one member in labor forece)
#delimit;
egen lf=rowtotal(lfp_new working);
bys yearhhnr: egen lfp_hh=total(lf);
keep if lfp_hh>0 & lfp_hh!=.;

* keep only the observation of head (i.e. 1 observation per household);
#delimit;
keep if head==1;

label var year "Jahr";

*****************************;
* gini coefficient;
****************************;
#delimit;
gen HHpreincOECDgini=.;
gen HHpostincOECDgini=.;
gen gini_year=.;

#delimit cr
*Include zeros
ineqdec0 HHpreincOECD [aweight=w11102], by(year) 
local n = 1
forvalues i = 1984/2013 {
replace HHpreincOECDgini= r(gini_`i') in `n'
replace gini_year= `i' in `n'
local ++n
}

ineqdec0 HHpostincOECD [aweight=w11102], by(year) 
local n = 1 
forvalues i = 1984/2013 {
replace HHpostincOECDgini= r(gini_`i') in `n'
replace gini_year= `i' in `n'
local ++n
}

keep  HHpreincOECDgini HHpostincOECDgini gini_year
rename gini_year year
drop if year==.

save "${MY_FIGURE_PATH}gininettobrutto.dta", replace

#delimit;
label var HHpreincOECDgini "Bruttohaushaltseinkommen";
label var HHpostincOECDgini "Nettohaushaltseinkommen";
label var year "Jahr";

graph twoway line  HHpreincOECDgini HHpostincOECDgini year,
xlabel(1984(4)2013)
ylabel(0.2(0.05)0.4, format(%4.2f))
xline(1990.5, lcolor(black)) 
ytitle("Gini-Koeffizient")
lwidth(medthick medthick medthick medthick) lpattern(solid dash) lcolor(black black)
title("Gini-Koeffizient: Netto- vs. Bruttoeinkommen");
graph save "${MY_FIGURE_PATH}HHincgini", replace;
graph export "${MY_FIGURE_PATH}HHincgini.ps", replace;

